<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Log;
class index extends Controller
{
	public function testr()
	{
		$url
		file_put_contents(RUNTIME_PATH.'webfiles/'.$.'.txt', $this->curl($url));
	}
	public function curl($url='http://ctext.org/xiao-jing/zh')
	{
		$ch=curl_init($url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_PROXY, "http://127.0.0.1:1080");  
        curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);  
		curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11");
		return $data = curl_exec($ch);
	}
	public function climbcontent()
	{
		set_time_limit(0);
		require EXTEND_PATH.'pq.php';
		$data =DB::query('select * from book where type=2 and content=?',['']); 
		foreach($data as $v)
		{
			echo $item = 'http://ctext.org/'.$v['href'];
			return $this->curl($item);
			\phpQuery::newDocumentFileHTML($item);
			echo $content = pq("tbody")->html();return;
			if(strpos($content,'using appears to have been involved in abuse')>0)
			{
				Log::write('Access Denied');
				return 'Access Denied';
			}
			return;
		}
		
	}
	public function climb()
	{
		set_time_limit(0);
		require EXTEND_PATH.'pq.php';
		$data = DB::query('SELECT * FROM `book` WHERE id not in(select parent from book where type=2 group by parent) and type=1 ');
		foreach($data as $v)
		{
			$ifstop = file_get_contents(EXTEND_PATH.'stop.php');
			if($ifstop==1)
			{
				return $v['name'].'的上一个';
			}
			Log::write(date('Y-m-m H:i:s',time()).'------'.$v['name'].'  start<br>');
			$item = 'http://ctext.org/'.$v['href'];
			\phpQuery::newDocumentFileHTML($item); 
			$content = pq("#content2")->html();
			if(strpos($content,'using appears to have been involved in abuse ')>0)
			{
				Log::write('Access Denied');
				return 'Access Denied';
			}
			preg_match('/<span class="etext opt.*?span>/',$content,$matches);
			if(isset($matches[0])&& !empty($matches[0]))
			{
				$comment = strip_tags($matches[0]);
				DB::execute('update book set comment=? where id=?',[$comment,$v['id']]);
			}
			// $content = explode('</div>　1.',$content)[1];
			preg_match_all('/<a href=".*?">.*?<\/a>/',$content,$matches2);
			foreach($matches2[0] as $vv)
			{
				// echo '<br>';
				// echo $vv[0];
				$str = trim(strip_tags($vv));

				//去掉跟随别的挤在一块的空白
				$str = preg_replace('/s(?=s)/', '', $str);

				//最后，去掉非space 的空白，用一个空格代替
				$str = preg_replace('/[nrt]/', ' ', $str);
				$name = $this->unicode_encode($str);
		
				$href = explode('"',$vv)[1];
				$exist = DB::query('select * from book where href=?',[$href]);
				if(empty($exist))
				{
					DB::execute('insert into book (name,type,href,parent) values(?,?,?,?)',[$name,2,$href,$v['id']]);
				}
				Log::write($name);
			}
			sleep(rand(4,15));
			// dump($matches2);
			Log::write(date('Y-m-m H:i:s',time()).'------'.$v['name'].'  finished<br>');
		}
		
		return 'good';
			require EXTEND_PATH.'pq.php';
			\phpQuery::newDocumentFileHTML('http://ctext.org/pre-qin-and-han/zh'); 
			$content = pq("#menu")->html();
			$res = explode('>系統統計</a><br></span></span>',$content)[1];
			$res = explode('class="menuitem',$res);
			$big = [];
			$key = '';
			$bigitem = '';
			$matches = '';
			foreach($res as $v)
			{
				if(strpos($v,'listhead')!==false)
				{
					$key = '';
				}
				preg_match('/href=.*?\/zh">.*?<\/a>/', $v,$matches);
				// print_r( $matches);
				if(!empty($matches))
				{
					$word = [];
					preg_match('/>.*?</',$matches[0],$word);
					$word = substr($word[0],1,-1);
					$link = explode('"',$matches[0])[1];
					if(empty($key))
					{
						$key = $word;
					}
					else if(preg_grep('/id="m.*?"/',[$v]))
					{
						$bigitem = $word;
							$bigiteminfo[$bigitem] = ['name'=>$word,'href'=>$link];
					}
					else
					{
						$big[$key][$bigitem][]=['name'=>$word,'href'=>$link];
					}
				}
				
			}
			// dump($bigiteminfo);return;
			// foreach($big as $k=>$v)
			// {
			// 	DB::execute('insert into book (name,parent)values(?,0)',[$k]);
			// 	$userId = Db::name('book')->getLastInsID();
			// 	foreach($v as $kk=>$vv)
			// 	{
			// 		DB::execute('insert into book (name,parent,href)values(?,?,?)',[$kk,$userId,$bigiteminfo[$kk]['href']]);	
			// 		$insertid = Db::name('book')->getLastInsId();
			// 		foreach($vv as $ii =>$jj)
			// 		{
			// 			DB::execute('insert into book (name,parent,href,type)value(?,?,?,1)',[$jj['name'],$insertid,$jj['href']]);
			// 		}				
			// 	}
			// 	// print_r($v);
				
			// }
			print_r($big);
			// print_r($res);
			return;
			$content = pq("#content3")->html();
			$res = explode('武英殿二十四史》本',$content)[1];
			$res = explode('<br>',$res);
			print_r($res);
			$big = array();
			$key = '';
			foreach($res as $v)
			{
				$matches = '';
				if(strpos($v,'etext')===false)
				{echo strip_tags($v);continue;
					preg_match('/>.*?<\/a>/', $matches);
					echo $matches;
					$key = '';
				}
			}
			// $content = preg_replace('/<span .*?顯示資料來源.*? span>/', '', $content);
			// preg_match_all('//',$content,$matches);
			// print_r($matches);
			// foreach($macthes as $k=>$v)
			// {

			// }
	}
	public function unicode_encode($name)  
{  
    $name = iconv('UTF-8', 'UCS-2', $name);  
    $len = strlen($name);  
    $str = '';  
    for ($i = 0; $i < $len - 1; $i = $i + 2)  
    {  
        $c = $name[$i];  
        $c2 = $name[$i + 1];  
        if (ord($c) > 0)  
        {    // 两个字节的文字  
            $str .= '\u'.base_convert(ord($c), 10, 16).base_convert(ord($c2), 10, 16);  
        }  
        else  
        {  
            $str .= $c2;  
        }  
    }  
    return $str;  
}  
public function unicode_decode($name)  
{  
    // 转换编码，将Unicode编码转换成可以浏览的utf-8编码  
    $pattern = '/([\w]+)|(\\\u([\w]{4}))/i';  
    preg_match_all($pattern, $name, $matches);  
    if (!empty($matches))  
    {  
        $name = '';  
        for ($j = 0; $j < count($matches[0]); $j++)  
        {  
            $str = $matches[0][$j];  
            if (strpos($str, '\\u') === 0)  
            {  
                $code = base_convert(substr($str, 2, 2), 16, 10);  
                $code2 = base_convert(substr($str, 4), 16, 10);  
                $c = chr($code).chr($code2);  
                $c = iconv('UCS-2', 'UTF-8', $c);  
                $name .= $c;  
            }  
            else  
            {  
                $name .= $str;  
            }  
        }  
    }  
    return $name;  
}  
}